define(function(require, exports, module){
	
	var uploadImg = require('uploadImg');
	var jq = $;
	
	jq.extend({
        os:{
            ios : false,
            android: false,
            version: false
        }
    });
	
	var ua = navigator.userAgent;
    var browser = {},
        weixin = ua.match(/MicroMessenger\/([^\s]+)/),
        webkit = ua.match(/WebKit\/([\d.]+)/),
        android = ua.match(/(Android)\s+([\d.]+)/),
        ipad = ua.match(/(iPad).*OS\s([\d_]+)/),
        ipod = ua.match(/(iPod).*OS\s([\d_]+)/),
        iphone = !ipod && !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/),
        webos = ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/),
        touchpad = webos && ua.match(/TouchPad/),
        kindle = ua.match(/Kindle\/([\d.]+)/),
        silk = ua.match(/Silk\/([\d._]+)/),
        blackberry = ua.match(/(BlackBerry).*Version\/([\d.]+)/),
        mqqbrowser = ua.match(/MQQBrowser\/([\d.]+)/),
        chrome = ua.match(/CriOS\/([\d.]+)/),
        opera = ua.match(/Opera\/([\d.]+)/),
        safari = ua.match(/Safari\/([\d.]+)/);
    if (weixin) {
       jq.os.wx = true;
       jq.os.wxVersion = weixin[1];
    }
    // if (browser.webkit = !! webkit) browser.version = webkit[1]
    if (android) {
        jq.os.android = true;
        jq.os.version = android[2];
    }
    if (iphone) {
        jq.os.ios = jq.os.iphone = true;
        jq.os.version = iphone[2].replace(/_/g, '.');
    }
    if (ipad) {
        jq.os.ios = jq.os.ipad = true;
        jq.os.version = ipad[2].replace(/_/g, '.');
    }
    if (ipod) {
        jq.os.ios = jq.os.ipod = true;
        jq.os.version = ipod[2].replace(/_/g, '.');
    }
	
	module.exports = {
	        uploadTimer: null,
	        // 上传相关
	        initUpload: function() {
	            // 上传图片的绑定
	            // jq('#addPic').on('click', function() {
	                // console.log(1);
	            // });

	            jq('#uploadFile, #fistUploadFile').on('click', function() {
	                var thisObj = jq(this);
	                if(jq('.photoList').find('#livideo').length > 0){
	                    jq.DIC.dialog({id: 'addWsTips', content: '图片和微视只能发一种哦~', autoClose: 2000});
	                    return false;                    
	                }
	                if (uploadImg.isBusy) {
	                    alert('上传中，请稍后添加');
	                    return false;
	                }
	                if(thisObj.attr('id') == 'fistUploadFile'){
	                    if(jq('.iconSendImg').hasClass('fail')){
	                        jq.DIC.dialog({content:'不能再上传了，最多只能上传8张图片哦~', autoClose:true});
	                        return false;
	                    }
	                }
	            });

	            jq('body').on('click', '.iconSendImg, .iconArrowR', function(e){
	                var thisObj = jq(this);
	                var photoList = jq('.photoList');
	                //点击图片图标
	                if(thisObj.hasClass('iconSendImg')){
	                    if(photoList.is(':hidden')){
	                        //jq('.sendCon').animate({height: '60'}, 300);
	                        jq('.sendCon').css('height', '60');
	                        photoList.show();
	                    }
	                }
	                //查看更多表情
	                if(thisObj.hasClass('iconArrowR')){
	                    var expressionMenu = jq('.expressionMenu').find('a');
	                    var haveMenuWidth = expressionMenu.length*76;
	                    var expressionTabWidth = jq('.expressionTab').width();
	                    if(haveMenuWidth > expressionTabWidth){
	                        var firstChild = jq(expressionMenu[0]);
	                        jq('.expressionMenu').append(firstChild.clone());
	                        firstChild.remove();
	                    }else{
	                        jq.DIC.dialog({id:'haveMoreExpression', content:'没有更多表情了哦~',autoClose:true});
	                    }
	                }

	            });
	            //首次点击图片的图标，触发一次手机的默认上传事件
	            jq('body').on('change', '#fistUploadFile', function(e){
	                var content = jq('#content')[0];
	                jq('.photoList').show();
	                jq('.operatList').hide();
	                jq('.photoTipsBox').show();
	                jq('.operatIcon').removeClass('on');
	                jq('.iconSendImg').addClass('on');
	                //jq('.sendCon').css('height', 'auto');
	                if(jq('.sendCon').height() != 60){
	                    //jq('.sendCon').animate({height: '60'}, 300);
	                    jq('.sendCon').css('height', '60');

	                }
	                //传图时输入框定位到底部
	                content.scrollTop = content.scrollHeight
	            });

	            // 文件表单发生变化时
	            jq('body').on('change', '#uploadFile, #fistUploadFile', function(e) {
	                //执行图片预览、压缩定时器
	                uploadTimer = setInterval(function() {
	                    // 预览
	                    setTimeout(function() {
	                        if (uploadImg.previewQueue.length) {
	                            var jobId = uploadImg.previewQueue.shift();
	                            uploadImg.uploadPreview(jobId);
	                        }
	                    }, 1);
	                    // 上传
	                    setTimeout(function() {
	                        if (!uploadImg.isBusy && uploadImg.uploadQueue.length) {
	                            var jobId = uploadImg.uploadQueue.shift();
	                            uploadImg.isBusy = true;
	                            uploadImg.createUpload(jobId, 'newthread', uploadTimer);
	                        }
	                    }, 10);
	                }, 300);

	                e = e || window.event;
	                var fileList = e.target.files;
	                if (!fileList.length) {
	                    return false;
	                }

	                for (var i = 0; i<fileList.length; i++) {
	                    if (uploadImg.countUpload() >= uploadImg.maxUpload) {
	                        jq.DIC.dialog({content:'你最多只能上传8张照片',autoClose:true});
	                        break;
	                    }

	                    var file = fileList[i];

	                    if (!uploadImg.checkPicType(file)) {
	                        jq.DIC.dialog({content: '上传照片格式不支持',autoClose:true});
	                        continue;
	                    }
	                    // console.log(file);
	                    if (!uploadImg.checkPicSize(file)) {
	                        jq.DIC.dialog({content: '图片体积过大', autoClose:true});
	                        continue;
	                    }

	                    var id = Date.now() + i;
	                    // 增加到上传对象中, 上传完成后，修改为 true
	                    uploadImg.uploadInfo[id] = {
	                        file: file,
	                        isDone: false,
	                    };

	                    var html = '<li id="li' + id + '"><div class="photoCut"><img src="http://dzqun.gtimg.cn/quan/images/defaultImg.png" class="attchImg" alt="photo"></div>' +
	                            '<div class="maskLay"></div>' +
	                            '<a href="javascript:;" class="cBtn cBtnOn pa db" title="" _id="'+id+'">关闭</a></li>';
	                    jq('#addPic').before(html);

	                    uploadImg.previewQueue.push(id);

	                    // 图片已经上传了 8 张，隐藏 + 号
	                    if (uploadImg.countUpload() >= uploadImg.maxUpload) {
	                        jq('#addPic').hide();
	                        jq('.iconSendImg').addClass('fail');
	                    }
	                    //更新剩余上传数
	                    setTimeout(function(){
	                        uploadImg.uploadRemaining();
	                    }, 400);

	                }

	                // 把输入框清空
	                jq(this).val('');
	            });

	            jq('.photoList').on('click', '.cBtn', function() {
	                // var result = confirm('取消上传这张图片?');
	                // if (!result) {
	                    // return false;
	                // }
	                var id = jq(this).attr('_id');
	                // 取消这个请求
	                if (uploadImg.xhr[id]) {
	                    uploadImg.xhr[id].abort();
	                }
	                // 图片删除
	                jq('#li' + id).remove();
	                // 表单中删除
	                jq('#input' + id).remove();
	                uploadImg.uploadInfo[id] = null;
	                //如果删除的微视，添加微视图标高亮
	                if(id == 'video'){
	                    jq('.iconVideo').addClass('iconVideoOn');
	                }
	                //如果删除的微视，添加微视图标高亮
	                if(id == 'video'){
	                    jq('.iconVideo').addClass('iconVideoOn');
	                }

	                // 图片变少了，显示+号
	                if (uploadImg.countUpload() < uploadImg.maxUpload) {
	                    jq('#addPic').show();
	                    jq('.iconSendImg').removeClass('fail');
	                }
	                //更新剩余上传数
	                uploadImg.uploadRemaining();

	                //当删除所有图片后隐藏添加图片的图标加号
	                if(jq('.photoList').find('li').length < 2){
	                    jq('.photoList').hide();
	                    jq('.sendCon').css('height', module.exports.contentHeight);
	                }
	            });

	            
	        },
	}
	
	module.exports.initUpload();
});

